package com.example.proto3;

import com.google.protobuf.Any;
import org.springframework.util.Assert;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
import java.util.Scanner;

/**
 * @auther hewenhui
 * @see <a href="hetp://www.codingmaster.com> java </a>
 */
public class TestWrite {
    public static void main(String[] args) throws IOException {
        // 创建构造器
        Contacts.Builder contactsBuilder = Contacts.newBuilder();
        // 读取本地已存在的contacts.bin 反序列化通讯录对象
//        Contacts contacts = Contacts.parseFrom(Files.newInputStream(Paths.get("src/main/java/com/example/proto3/contacts.bin")));
//        contactsBuilder = contacts.toBuilder();
        try{
            contactsBuilder.mergeFrom(
                    Files.newInputStream(Paths.get("src/main/java/com/example/proto3/contacts.bin")));
        }catch (NoSuchFileException e){
            System.out.println("无、创建");
        }
        contactsBuilder.addContacts(addPeopleInfo());
        FileOutputStream outputStream = new FileOutputStream(
                "src/main/java/com/example/proto3/contacts.bin");
        contactsBuilder.build().writeTo(outputStream);
        outputStream.close();
    }

    private static PeopleInfo addPeopleInfo() {
        Scanner scanner = new Scanner(System.in);
        PeopleInfo.Builder builder = PeopleInfo.newBuilder();
        System.out.println("=========新增联系人=========");

        System.out.print("请输入联系人姓名：");
        String name = scanner.nextLine();
        builder.setName(name);

        System.out.print("请输入联系人年龄：");
        int age = scanner.nextInt();
        scanner.nextLine();
        builder.setAge(age);

        for (int i = 0; ; i++) {
            System.out.print("请输入联系人电话"+(i+1)+"，只输入回车结束电话新增：");
            String number = scanner.nextLine();
            if(number.isEmpty()) {
                break;
            }
            PeopleInfo.Phone.Builder phoneBuilder = PeopleInfo.Phone.newBuilder();
            phoneBuilder.setNumber(number);
            System.out.print("电话类型：（1、移动电话  2、固定电话）");
            int type = scanner.nextInt();
            scanner.nextLine();
            switch (type){
                case 1:
                    phoneBuilder.setType(PeopleInfo.Phone.PhoneType.MP);
                    break;
                case 2:
                    phoneBuilder.setType(PeopleInfo.Phone.PhoneType.TEL);
                    break;
                default:
                    System.out.println("输入有误！");
                    break;
            }
            builder.addPhone(phoneBuilder);
        }

        Address.Builder addressBuilder = Address.newBuilder();
        System.out.print("请输入联系人家庭地址：");
        String homeAddress = scanner.nextLine();
        addressBuilder.setHomeAddress(homeAddress);
        System.out.print("请输入联系人单位地址：");
        String unitAddress = scanner.nextLine();
        addressBuilder.setUnitAddress(unitAddress);
        builder.setData(Any.pack(addressBuilder.build()));

        System.out.print("请选择要输入的其他联系方式（1、QQ  2、微信）：");
        int select = scanner.nextInt();
        scanner.nextLine();
        String otherNumber = scanner.nextLine();
        switch (select){
            case 1:
                System.out.print("请输入号码：");
                builder.setQq(otherNumber);
                break;
            case 2:
                System.out.print("请输入号码：");
                builder.setWechat(otherNumber);
                break;
            default:
                System.out.println("不设置其他联系方式");
                break;
        }

        System.out.println("添加备注标题:（回车可停止添加）");
        System.out.println("请输入备注标题:（回车可停止添加）");
        String title = scanner.nextLine();
        while (!title.isEmpty()){
            System.out.println("请输入备注信息:");
            String content = scanner.nextLine();
            builder.putRemark(title,content);
            System.out.println("请输入备注标题:（回车可停止添加）");
            title = scanner.nextLine();
        }
        System.out.println("==========添加联系人结束==========");
        return builder.build();
    }
}
